/*
 * Copyright 2007, 2008 Tiberiumlabs
 *
 * This file is part of Tiberiumlabs Shards.
 *
 * Tiberiumlabs Shards is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Tiberiumlabs Shards is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.tiberiumlabs.swing.greymetal;

import java.awt.Color;
import java.awt.Font;
import java.util.Arrays;
import java.util.List;
import javax.swing.ToolTipManager;
import javax.swing.UIDefaults;
import javax.swing.plaf.ColorUIResource;
import javax.swing.plaf.FontUIResource;
import javax.swing.plaf.metal.OceanTheme;

/**
 *
 * @author <a href="paranoid.tiberiumlabs@gmail.com">Paranoid</a>
 */
public class GreyMetalTheme extends OceanTheme {

    private static final ColorUIResource GRAY_DARK = new ColorUIResource(192, 202, 209);
    private static final ColorUIResource GRAY_BLACK = new ColorUIResource(100, 110, 120);

    @Override
    public void addCustomEntriesToTable(UIDefaults table) {

        System.setProperty("awt.useSystemAAFontSettings", "on");
        System.setProperty("swing.boldMetal", "false");

        super.addCustomEntriesToTable(table);

        Color grayLighter = new ColorUIResource(223, 228, 231);
        Color yellow = new Color(255, 255, 220);

        List buttonGradient = Arrays.asList(
                new Object[] {new Float(0f), new Float(0f),
                              grayLighter, Color.WHITE, GRAY_DARK});
        List focusGradient = Arrays.asList(
                new Object[] {new Float(.3f), new Float(.2f),
                              GRAY_DARK, grayLighter, GRAY_BLACK});

        Object[] defaults = new Object[] {
            "swing.boldMetal", Boolean.FALSE,

            // set gradient:
            "Button.gradient", buttonGradient,
            "CheckBox.gradient", buttonGradient,
            "CheckBoxMenuItem.gradient", buttonGradient,
            "InternalFrame.activeTitleGradient", buttonGradient,
            "RadioButton.gradient", buttonGradient,
            "RadioButtonMenuItem.gradient", buttonGradient,
            "ScrollBar.gradient", buttonGradient,
            "ToggleButton.gradient", buttonGradient,
            "Slider.gradient", buttonGradient,
            "Slider.focusGradient", focusGradient,

            // elements colors:
            "TextField.focused", yellow,
            "TextField.selectionForeground", yellow,
            "TextField.selectionBackground", GRAY_BLACK,

            "PasswordField.selectionForeground", yellow,
            "PasswordField.selectionBackground", GRAY_BLACK,

            "TextArea.selectionForeground", yellow,
            "TextArea.selectionBackground", GRAY_BLACK,

            "ToolTip.background", yellow,

            "Table.gridColor", GRAY_DARK,
            "Table.selectionForeground", yellow,
            "Table.selectionBackground", GRAY_BLACK,

            "Tree.selectionForeground", yellow,
            "Tree.selectionBackground", GRAY_BLACK,

            "List.selectionForeground", yellow,
            "List.selectionBackground", GRAY_BLACK,

            "ComboBox.selectionForeground", yellow,
            "ComboBox.selectionBackground", GRAY_BLACK,
            "ComboBox.background", grayLighter,

            "ProgressBar.foreground", GRAY_BLACK, // filler color
            "ProgressBar.selectionForeground", yellow, // color of text on filled part
            "ProgressBar.selectionBackground", GRAY_BLACK, // color of text on clear part

            "Menu.selectionBackground", GRAY_BLACK,
            "Menu.selectionForeground", yellow,

            "MenuItem.selectionBackground", GRAY_BLACK,
            "MenuItem.selectionForeground", yellow,

            "RadioButtonMenuItem.selectionBackground", GRAY_BLACK,
            "RadioButtonMenuItem.selectionForeground", yellow,

            "CheckBoxMenuItem.selectionBackground", GRAY_BLACK,
            "CheckBoxMenuItem.selectionForeground", yellow,

            // JTabbedPane background:
            "TabbedPane.contentAreaColor", grayLighter,
            "TabbedPane.selected", grayLighter,

            // TODO check why UIManager ignores this key
            "Table.dropCellBackground", GRAY_DARK,
            "Table.dropCellForeground", yellow
        };

        table.putDefaults(defaults);

        ToolTipManager ttm = ToolTipManager.sharedInstance();
        ttm.setLightWeightPopupEnabled(true);
        ttm.setInitialDelay(250);
        ttm.setDismissDelay(5000);
    }

    @Override
    protected ColorUIResource getPrimary1() {
        return GRAY_BLACK;
    }

    @Override
    protected ColorUIResource getPrimary2() {
        // TODO combo boxes looks not very good
        return super.getPrimary2();
    }

    @Override
    protected ColorUIResource getPrimary3() {
        // TODO choose better color, because JTree ass-ugly
        return super.getPrimary3();
    }

    @Override
    protected ColorUIResource getSecondary2() {
        return GRAY_DARK;
    }

    private static final String[] fontNames = {
        Font.SANS_SERIF, Font.SANS_SERIF, Font.SANS_SERIF, Font.SANS_SERIF, Font.SANS_SERIF, Font.SANS_SERIF
    };

    private static final int[] fontStyles = {
        Font.PLAIN, Font.PLAIN, Font.PLAIN, Font.PLAIN, Font.BOLD, Font.PLAIN
    };

    private static final int[] fontSizes = {
        12, 12, 12, 12, 12, 10
    };
    private static final FontUIResource[] fonts = new FontUIResource[6];

    @Override
    public FontUIResource getControlTextFont() {
        return getFont(0);
    }

    @Override
    public FontUIResource getMenuTextFont() {
        return getFont(3);
    }

    @Override
    public FontUIResource getSubTextFont() {
        return getFont(5);
    }

    @Override
    public FontUIResource getSystemTextFont() {
        return getFont(1);
    }

    @Override
    public FontUIResource getUserTextFont() {
        return getFont(2);
    }

    @Override
    public FontUIResource getWindowTitleFont() {
        return getFont(4);
    }

    private FontUIResource getFont(int type) {
        if (fonts[type] == null) {
            Font f = new Font(fontNames[type], fontStyles[type], fontSizes[type]);
            fonts[type] = new FontUIResource(f);
        }
        return fonts[type];
    }

}
